package com.heatup.apt.common.util.Netty;

import io.netty.buffer.ByteBuf;
import io.netty.channel.ChannelHandlerContext;
import io.netty.handler.codec.ByteToMessageDecoder;
import io.netty.handler.codec.TooLongFrameException;

import java.util.List;

/**
 * 1. 继承 ByteToMessageDecoder 用于解码入站字节到消息
 * 2. 指定最大需要的帧产生的体积
 * 3. 如果帧太大就丢弃并抛出一个 TooLongFrameException 异常
 * 4. 同时从 ByteBuf 读到新帧
 * 5. 添加帧到解码消息 List
 *
 * Created by blank on 2017/11/20 下午2:21
 */
public class FrameChunkDecoder extends ByteToMessageDecoder {//1

    private final int maxFrameSize;

    public FrameChunkDecoder(int maxFrameSize) {
        this.maxFrameSize = maxFrameSize;
    }

    @Override
    protected void decode(ChannelHandlerContext ctx, ByteBuf in, List<Object> out) throws Exception {

        int readableBytes = in.readableBytes(); //2
        if (readableBytes > maxFrameSize) {
            //discard the bytes //3
            in.clear();
            throw new TooLongFrameException();
        }
        ByteBuf buf = in.readBytes(readableBytes); //4
        out.add(buf); //5

    }
}
